﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using Plasmoid.Extensions;
using ReversiFrontend;

namespace ReversiFrontend
{
    class ImageButton : Button
    {
        private Image OrigBackgroundImage, LightBackgroundImage, DarkBackgroundImage;
        public ImageButton()
        {

        }

        protected override void OnBackgroundImageChanged(EventArgs e)
        {
            base.OnBackgroundImageChanged(e);

            if (BackgroundImage != OrigBackgroundImage && BackgroundImage != LightBackgroundImage && BackgroundImage != DarkBackgroundImage)
            {
                OrigBackgroundImage = BackgroundImage;

                LightBackgroundImage = AdjustBackgroundImage(new Bitmap((BackgroundImage.Clone() as Image)), 1.3f) as Image;
                DarkBackgroundImage = AdjustBackgroundImage(new Bitmap((BackgroundImage.Clone() as Image)), 0.7f) as Image;
            }
        }    
        
        #region MouseEvent handler
        protected override void OnMouseMove(MouseEventArgs mevent)
        {
            base.OnMouseMove(mevent);
            if (mevent.Button == System.Windows.Forms.MouseButtons.Left)
            {
                BackgroundImage = DarkBackgroundImage;
            }
            else
            {
                BackgroundImage = LightBackgroundImage;
            }
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            BackgroundImage = OrigBackgroundImage;
        }
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            BackgroundImage = DarkBackgroundImage;
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            BackgroundImage = LightBackgroundImage;
        }
        #endregion

        private Bitmap AdjustBackgroundImage(Bitmap originalBackgroundImage, 
                                float brightness = 1.0f, // no change in brightness
                                float contrast = 1.0f, // twice the contrast
                                float gamma = 1.0f) // no change in gamma 
        {            
            Bitmap adjustedBackgroundImage = new Bitmap(originalBackgroundImage.Width, originalBackgroundImage.Height);
            

            float adjustedBrightness = brightness - 1.0f;
            // create matrix that will brighten and contrast the BackgroundImage
            float[][] ptsArray ={
                    new float[] {contrast, 0, 0, 0, 0}, // scale red
                    new float[] {0, contrast, 0, 0, 0}, // scale green
                    new float[] {0, 0, contrast, 0, 0}, // scale blue
                    new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha
                    new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}};

            var BackgroundImageAttributes = new ImageAttributes();
            BackgroundImageAttributes.ClearColorMatrix();
            BackgroundImageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            BackgroundImageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap);
            Graphics g = Graphics.FromImage(adjustedBackgroundImage);
            g.DrawImage(originalBackgroundImage, new Rectangle(0, 0, adjustedBackgroundImage.Width, adjustedBackgroundImage.Height)
                ,0,0,originalBackgroundImage.Width,originalBackgroundImage.Height,
                GraphicsUnit.Pixel, BackgroundImageAttributes);

           // adjustedBackgroundImage.MakeTransparent(adjustedBackgroundImage.GetPixel(0, 0));
            return adjustedBackgroundImage;
        }

    }
}
